<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="讲师名称">
        <el-input v-model="teacher.name" />
      </el-form-item>
      <el-form-item label="入驻时间">
        <el-date-picker v-model="teacher.joinDate" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort" :min="0"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
            -->
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <!-- TODO: 讲师头像 -->
      <el-form-item label="讲师头像">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="uploadPath"
          class="avatar-uploader">
          <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar" width="200px">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro"/>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career" :rows="10" type="textarea"/>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher'
export default {
  data() {
    return {
      // 初始化讲师默认数据
      uploadPath: 'http://localhost:8120/admin/oss/upload?module=avatar',
      teacher: {
        sort: 0,
        level: 1
      },
      saveBtnDisabled: false // 保存按钮是否禁用，防止表单重复提交
    }
  },
  created() {
    if (this.$route.params.id) {
      var id = this.$route.params.id
      this.queryById(id)
    }
  },
  methods: {
    //
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('图片的格式只能是JPG！')
      }
      if (!isLt2M) {
        this.$message.error('图片大小不能超过2M')
      }
      return isJPG && isLt2M
    },

    //
    handleAvatarSuccess(res, file) {
      if (res.success) {
        this.teacher.avatar = res.data.fileURL
        this.$forceUpdate()
      } else {
        this.$message.error('头像上传失败')
      }
    },

    // 清空taecher
    clear() {
      this.teacher = {
        sort: 0,
        level: 1
      }
    },
    saveOrUpdate() {
      if (this.teacher.id) {
        this.update()
      } else {
        this.save()
      }
    },
    save() {
      teacherApi.save(this.teacher).then(r => {
        this.$message.success('新增成功')
        this.$router.push({ path: '/teacher/list' })
      })
    },
    update() {
      teacherApi.update(this.teacher).then(r => {
        this.$message.success('更新成功')
        this.$router.push('/teacher/list')
      })
    },
    queryById(id) {
      teacherApi.queryById(id).then(r => {
        this.teacher = r.data.teacher
      })
    }
  }
}
</script>
